package com.richest.api.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.richest.api.controller.form.OrderForm;
import com.richest.api.controller.form.OrderSubBanquetForm;
import com.richest.api.controller.form.OrderSubCommodityForm;
import com.richest.api.controller.form.OrderSubDiscountForm;
import com.richest.api.controller.form.OrderSubForm;
import com.richest.api.controller.form.OrderSubFullReductionForm;
import com.richest.api.dao.OrderMasterMapper;
import com.richest.api.dao.OrderSubBanquetMapper;
import com.richest.api.dao.OrderSubCommodityMapper;
import com.richest.api.dao.OrderSubDiscountMapper;
import com.richest.api.dao.OrderSubFullReductionMapper;
import com.richest.api.dao.OrderSubMapper;
import com.richest.api.service.IClientOrderService;
import com.richest.api.utils.OrderFormConvertUtil;
import com.richest.bean.OrderMasterBean;
import com.richest.bean.OrderSubBanquetBean;
import com.richest.bean.OrderSubBean;
import com.richest.bean.OrderSubCommodityBean;
import com.richest.bean.OrderSubDiscountBean;
import com.richest.bean.OrderSubFullReductionBean;

/**
 * 客户端调用 订单功能服务接口实现
 * 
 * @author 杨博
 * @version richest-api 2.0
 */
@Service
public class ClientOrderServiceImpl implements IClientOrderService {

	@Autowired
	private OrderMasterMapper masterMapper;

	@Autowired
	private OrderSubMapper subMapper;

	@Autowired
	private OrderSubBanquetMapper subBanquetMapper;

	@Autowired
	private OrderSubCommodityMapper subCommodityMapper;

	@Autowired
	private OrderSubDiscountMapper discountMapper;

	@Autowired
	private OrderSubFullReductionMapper fullReductionMapper;

	@Transactional
	@Override
	public Map<String, Object> placeOrder(OrderForm orderForm) {
		Map<String, Object> map = new HashMap<>();
		/**
		 * 转换为主订单Bean
		 */
		OrderMasterBean masterBean = OrderFormConvertUtil.toOrderMasterBean(orderForm);
		masterBean.setOrder_master_status_id(7L);
		Long orderMasterId = masterMapper.orderMasterAdd(masterBean);
		map.put("OrderMasterId", orderMasterId);
		OrderSubForm subForm = orderForm.getSubForm();
		if (subForm != null) {
			OrderSubBean subBean = OrderFormConvertUtil.toOrderSubBean(subForm);
			subBean.setOrder_master_id(orderMasterId);
			subBean.setOrder_sub_status_id(25L);
			Long orderSubid = subMapper.orderSubAdd(subBean);
			map.put("OrderSubId", orderSubid);
			// 获取子订单中包含的包房桌数据列表
			List<OrderSubBanquetForm> banquetForms = subForm.getBanquetForms();
			if (banquetForms != null) {
				for (OrderSubBanquetForm orderSubBanquetForm : banquetForms) {
					OrderSubBanquetBean subBanquetBean = OrderFormConvertUtil
							.toOrderSubBanquetBean(orderSubBanquetForm);
					subBanquetBean.setOrder_sub_id(orderSubid);
					Long orderSubBanquetId = subBanquetMapper.orderSubBanquetAdd(subBanquetBean);
					map.put("OrderSubBanquetId", orderSubBanquetId);
				}
			}
			// 获取子订单中包含的商品数据列表
			List<OrderSubCommodityForm> commodityForms = subForm.getCommodityForms();
			if (commodityForms != null) {
				for (OrderSubCommodityForm orderSubCommodityForm : commodityForms) {
					OrderSubCommodityBean subCommodityBean = OrderFormConvertUtil
							.toOrderSubCommodityBean(orderSubCommodityForm);
					subCommodityBean.setOrder_sub_id(orderSubid);
					Long orderSubCommodityId = subCommodityMapper.orderSubCommodityAdd(subCommodityBean);
					map.put("OrderSubcommodityId", orderSubCommodityId);
				}
			}
			// 获取子订单中用户使用的优惠信息
			OrderSubDiscountForm discountForm = subForm.getDiscountForm();
			if (discountForm != null) {
				OrderSubDiscountBean discountBean = new OrderSubDiscountBean();
				discountBean.setUser_discount_id(discountForm.getUser_discount_id());
				discountBean.setOrder_sub_id(orderSubid);
				Long orderSubDiscountId = discountMapper.orderSubDiscountAdd(discountBean);
				map.put("OrderSubDiscountId", orderSubDiscountId);
			}
			// 获取子订单中用户使用的满减策略
			OrderSubFullReductionForm fullReductionForm = subForm.getFullReductionForm();
			if (fullReductionForm != null) {
				OrderSubFullReductionBean fullReductionBean = new OrderSubFullReductionBean();
				fullReductionBean.setFull_reduction_id(fullReductionForm.getFull_reduction_id());
				fullReductionBean.setOrder_sub_id(orderSubid);
				Long orderSubFullReductionId = fullReductionMapper.orderSubFullReductionAdd(fullReductionBean);
				map.put("OrderSubFullReductionId", orderSubFullReductionId);
			}
		}

		return map;
	}

	@Override
	public Boolean cancelMasterOrder(Long orderMasterId, Long StatusId, Long userId, String remarks) {
		Long newStatustId = 0L;
		switch (StatusId.toString()) {
		case "12":
			newStatustId = 20L;
			break;
		case "13":
			newStatustId = 19L;
			break;
		default:
			return false;
		}
		Map<String, Object> params = new HashMap<>();
		params.put("order_master_status_id", newStatustId);
		params.put("modify_user_id", userId);
		params.put("id", orderMasterId);
		params.put("remarks", remarks);
		int value = masterMapper.orderMasterUpdateStatus(params);
		if (value == 1) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Boolean cancelSubOrder(Long orderSubId, Long StatusId, Long userId, String remarks) {
		Long newStatustId = 0L;
		switch (StatusId.toString()) {
		case "25":
		case "26":
			newStatustId = 27L;
			break;
		case "28":
			newStatustId = 31L;
			break;
		default:
			return false;
		}
		Map<String, Object> params = new HashMap<>();
		params.put("order_master_status_id", newStatustId);
		params.put("modify_user_id", userId);
		params.put("examine_time", null);
		params.put("id", orderSubId);
		params.put("remarks", remarks);
		int value = subMapper.orderSubUpdateStatus(params);
		if (value == 1) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Boolean userConfirmationCancelMasterOrder(Long orderMasterId, Long StatusId, Long userId, String remarks) {
		Long newStatustId = 0L;
		switch (StatusId.toString()) {
		case "21":
			newStatustId = 22L;
			break;
		default:
			return false;
		}
		Map<String, Object> params = new HashMap<>();
		params.put("order_master_status_id", newStatustId);
		params.put("modify_user_id", userId);
		params.put("id", orderMasterId);
		params.put("remarks", remarks);
		int value = masterMapper.orderMasterUpdateStatus(params);
		if (value == 1) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Boolean userConfirmationCancelSubOrder(Long orderSubId, Long StatusId, Long userId, String remarks) {
		Long newStatustId = 0L;
		switch (StatusId.toString()) {
		case "33":
			newStatustId = 34L;
			break;
		default:
			return false;
		}
		Map<String, Object> params = new HashMap<>();
		params.put("order_master_status_id", newStatustId);
		params.put("modify_user_id", userId);
		params.put("id", orderSubId);
		params.put("remarks", remarks);
		int value = subMapper.orderSubUpdateStatus(params);
		if (value == 1) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Boolean finishMasterOrder(Long orderMasterId, Long StatusId, Long userId, String remarks) {
		Long newStatustId = 0L;
		switch (StatusId.toString()) {
		case "16":
			newStatustId = 17L;
			break;
		default:
			return false;
		}
		Map<String, Object> params = new HashMap<>();
		params.put("order_master_status_id", newStatustId);
		params.put("modify_user_id", userId);
		params.put("id", orderMasterId);
		params.put("remarks", remarks);
		int value = masterMapper.orderMasterUpdateStatus(params);
		if (value == 1) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Boolean finishSubOrder(Long orderSubId, Long StatusId, Long userId, String remarks) {
		Long newStatustId = 0L;
		switch (StatusId.toString()) {
		case "28":
			newStatustId = 35L;
			break;
		default:
			return false;
		}
		Map<String, Object> params = new HashMap<>();
		params.put("order_master_status_id", newStatustId);
		params.put("modify_user_id", userId);
		params.put("id", orderSubId);
		params.put("remarks", remarks);
		int value = subMapper.orderSubUpdateStatus(params);
		if (value == 1) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public OrderMasterBean orderMasterSelectOneById(Long id) {
		return masterMapper.orderMasterSelectOne(id);
	}

	@Override
	public List<OrderMasterBean> orderMasterSelectListByUserId(Long userId, Integer beginNumber, Integer endNumber) {
		Map<String, Object> params = new HashMap<>();
		params.put("column", "user_id");
		params.put("value", userId);
		params.put("beginNumber", beginNumber);
		params.put("endNumber", endNumber);
		return masterMapper.orderMasterSelectList(params);
	}

	@Override
	public OrderSubBean orderSubSelectOneById(Long id) {
		return subMapper.orderSubSelectOne(id);
	}

	@Override
	public List<OrderSubBean> orderSubSelectListByOrderMasterId(Long orderMasterId) {
		Map<String, Object> params = new HashMap<>();
		params.put("column", "order_master_id");
		params.put("value", orderMasterId);
		params.put("beginNumber", 0);
		params.put("endNumber", 0);
		return subMapper.orderSubSelectList(params);
	}

	@Override
	public OrderSubBanquetBean orderSubBanquetSelectOneById(Long id) {
		return subBanquetMapper.orderSubBanquetSelectOne(id);
	}

	@Override
	public List<OrderSubBanquetBean> orderSubBanquetSelectListByOrderSubId(Long orderSubId) {
		Map<String, Object> params = new HashMap<>();
		params.put("column", "order_sub_id");
		params.put("value", orderSubId);
		params.put("beginNumber", 0);
		params.put("endNumber", 0);
		return subBanquetMapper.orderSubBanquetSelectList(params);
	}

	@Override
	public OrderSubCommodityBean orderSubCommoditySelectOneById(Long id) {
		return subCommodityMapper.orderSubCommoditySelectOne(id);
	}

	@Override
	public List<OrderSubCommodityBean> orderSubCommoditySelectListByOrderSubId(Long orderSubId) {
		Map<String, Object> params = new HashMap<>();
		params.put("column", "order_sub_id");
		params.put("value", orderSubId);
		params.put("beginNumber", 0);
		params.put("endNumber", 0);
		return subCommodityMapper.orderSubCommoditySelectList(params);
	}

	@Override
	public OrderSubDiscountBean orderSubDiscountSelectOneById(Long id) {
		return discountMapper.orderSubDiscountSelectOne(id);
	}

	@Override
	public List<OrderSubDiscountBean> orderSubDiscountSelectListByOrderSubId(Long orderSubId) {
		Map<String, Object> params = new HashMap<>();
		params.put("column", "order_sub_id");
		params.put("value", orderSubId);
		params.put("beginNumber", 0);
		params.put("endNumber", 0);
		return discountMapper.orderSubDiscountSelectList(params);
	}

	@Override
	public OrderSubFullReductionBean orderSubFullReductionSelectOneById(Long id) {
		return fullReductionMapper.orderSubFullReductionSelectOne(id);
	}

	@Override
	public List<OrderSubFullReductionBean> orderSubFullReductionSelectListByOrderSubId(Long orderSubId) {
		Map<String, Object> params = new HashMap<>();
		params.put("column", "order_sub_id");
		params.put("value", orderSubId);
		params.put("beginNumber", 0);
		params.put("endNumber", 0);
		return fullReductionMapper.orderSubFullReductionSelectList(params);
	}

}
